<template>
	<view>
		<nav-header title="编辑邀请函" :show-right="false">
		</nav-header>

		<view class="name">
			<view class="form">
				<uni-forms ref="form" :modelValue="formData" :rules="rules">
					<uni-forms-item name="name" label="名称:">
						<uni-easyinput type="text" @input="sumFontNum1" maxlength="10" v-model="formData.name"
							placeholder="请输入名称" :inputBorder="false">
							<template v-slot:right>
								<view class="tip">
									<text>{{fontNum1}}/10</text>
								</view>
							</template>
						</uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="title" label="标题:">
						<uni-easyinput type="text" @input="sumFontNum" maxlength="30" v-model="formData.title"
							placeholder="请输入标题" :inputBorder="false">
							<template v-slot:right>
								<view class="tip">
									<text>{{fontNum}}/30</text>
								</view>
							</template>
						</uni-easyinput>
					</uni-forms-item>
					<uni-forms-item name="content" label="内容:">
						<uni-easyinput type="textarea"  @input="sumFontNum2" maxlength="300" v-model="formData.content"
							placeholder="请输入内容" :inputBorder="false" />
					</uni-forms-item>
				</uni-forms>
				<view class="tip">
					<text>{{fontNum2}}/300</text>
				</view>
			</view>
			<view class="commit">
				<view class="button-style">
					<button class="btncolor" @click="submitForm">
						<text class="text-md-Medium" style="color: #fff;">修改</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					name: "",
					title: "",
					content: ""
				},
				fontNum1: 0,
				fontNum: 0,
				fontNum2: 0,
				groupId: 0,
				type: "",
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入名称',
						}]
					},
					title: {
						rules: [{
							required: true,
							errorMessage: '请输入标题',
						}]
					},
					content: {
						rules: [{
							required: true,
							errorMessage: '请输入内容',
						}]
					},
				},
			}
		},
		methods: {
			getData() {
				this.$http({
					url: "/inviation/get?groupId=" + this.groupId,
					method: "GET",
				}).then((res) => {
					console.log('getData', res)
					this.fontNum1 = res.name.length
					this.fontNum = res.title.length
					this.fontNum2 = res.content.length
					this.formData.name = res.name
					this.formData.title = res.title
					this.formData.content = res.content
				})
			},
			sumFontNum1(e) {
				this.fontNum1 = e.length
			},
			sumFontNum(e) {
				this.fontNum = e.length
			},
			sumFontNum2(e) {
				this.fontNum2 = e.length
			},
			submitForm() {
				this.$refs.form.validate().then(res => {
					let data = {
						groupId: this.groupId,
						name: this.formData.name,
						title: this.formData.title,
						content: this.formData.content
					}
					this.$http({
						url: "/inviation/save",
						method: "POST",
						data: data
					}).then((data) => {
						console.log(data)
						uni.showToast({
							title: "修改成功",
							icon: 'none'
						})
						setTimeout(() => {
							// 回退并刷新
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							prevPage.$vm.loadGroupInfo();
							uni.navigateBack();
						}, 1000);
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
		},
		onLoad(options) {
			this.groupId = options.id
			this.getData()
		},
	}
</script>

<style lang="scss" scoped>
	.name {
		width: 100%;

		.form {
			padding: 30rpx;

			:deep(.uni-easyinput__content-input) {
				// padding-left: 0 !important;
				padding-top: 5rpx !important;
			}

			:deep(.uni-easyinput__content.is-textarea) {
				// height: 40rpx;
				padding: 10rpx 24rpx;
				background-color: $gray-100 !important;
				height: 600rpx;
			}
			
			:deep(.uni-easyinput__content-textarea){
				height: 540rpx;
			}

			.tip {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				font-size: 25rpx;
				color: #aaa;
			}

			.edit-count {
				color: #5f5f5f;
				font-size: 30rpx;

				.kr {
					color: #d90000;
					margin-left: 20rpx;
				}
			}
		}

		// .commit {
		// 	button {
		// 		background-color: #0389ff;
		// 		color: #fff;
		// 		width: 92%;
		// 		height: 75rpx;
		// 		line-height: 75rpx;
		// 		font-size: 30rpx;
		// 	}
		// }

		.commit {
			width: 100%;
			position: fixed;
			bottom: 42rpx;
			left: 0;
			display: flex;
			justify-content: center;
			align-items: center;

			.button-style {
				padding: 24rpx 32rpx;
				width: 100%;
			}

			.btncolor {
				width: 100%;
				background-color: $brand-500;
				color: $white;
				border-radius: 34rpx;
				padding: 8rpx 24rpx;
				line-height: 60rpx;
				text-align: center;
			}
		}
	}
</style>